<?php
/**
 * vod
 * 点播
 * @author Administrator
 * @version
 */
class Model_Tables_Vod extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'vod';
    /**
     * add
     * 增加点播
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'title' => $params['title'],
		'type' => $params['type'],
		'director' => $params['director'],
		'act' => $params['act'],
		'tag1' => $params['tag1'],
		'tag2' => $params['tag2'],
		'tag3' => $params['tag3'],
		'imgurl' => $params['imgurl'],
		'state' => 1,//$params['state'],
		'mark' => $params['mark'],
		'bitrate' => $params['bitrate'],
		'resolution' => $params['resolution'],
		'duration' => $params['duration'],
		'content' => $params['content'],
        'updatetime'=>time(),
        'ad' => $params['ad'],
        'vodip' => $params['vodip'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑点播
     * @param $params 字段信息
     * @param $vid 主键值
     */
    public function modify( $params, $vid )
    {
        $data = array(
		'title' => $params['title'],
		'type' => $params['type'],
		'director' => $params['director'],
		'act' => $params['act'],
		'tag1' => $params['tag1'],
		'tag2' => $params['tag2'],
		'tag3' => $params['tag3'],
		'imgurl' => $params['imgurl'],
		'state' => $params['state'],
		'mark' => $params['mark'],
		'bitrate' => $params['bitrate'],
		'resolution' => $params['resolution'],
		'duration' => $params['duration'],
		'content' => $params['content'],
        'updatetime'=>time(),
        'ad' => $params['ad'],
        'vodip' => $params['vodip'],
        );
        $where = 'vid = '.$vid ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑点播
     * @param $vid 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $vid, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value,"updatetime"=>time());
    	$where = $db->quoteInto("vid=?", $vid);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转点播
     * @param $field 需要反转的字段
     * @param $vid 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update vod set  $field =not  $field, updatetime = ".time()."  where vid=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除点播
     * @param $vid 主键值
     */
    public function del( $vid )
    {
     	$where = 'vid = '.$vid;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取点播信息
     * @param $vid 主键值
     * @return 结果数组
     */
    public function getInfo($vid)
    {
        $row = $this->fetchRow('vid = ' . $vid);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取点播数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("title like ?", "%$s%");
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取点播数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('updatetime desc','vid desc');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("title like ?", "%$s%");
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    public function getList4Search($ids)
    {
    	$db = $this->getAdapter();
    	$result = $db->fetchAll("select * from vod where vid in($ids)");
    	return $result;
    }
    
    public function getCountbyTags($type,$tag1,$tag2,$tag3){
    	$db = $this->getAdapter();
    	$type = intval($type);
    	$select = $this->select()->from($this->_name,'count(*) as num');
    	$where = " type=$type ";    	
    	if($tag1!="全部")
			$where .= $db->quoteInto(" and tag1 like ?", "%$tag1%");
		if($tag2!="全部")
			$where .= $db->quoteInto(" and tag2 like ?", "%$tag2%");
		if($tag3!="全部")
			$where .= $db->quoteInto(" and tag3 like ?", "%$tag3%");
    	$select = $select->where($where);
        $result = $this->fetchRow($select)->num;
        return $result;
    }
 	public function getListbyTags($type,$tag1,$tag2,$tag3,$page,$numPerPage = 10){
    	$db = $this->getAdapter();
    	$type = intval($type);
 		$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('orderid desc','updatetime desc');
        $where = " type=$type ";    	
    	if($tag1!="全部")
			$where .= $db->quoteInto(" and tag1 like ?", "%$tag1%");
		if($tag2!="全部")
			$where .= $db->quoteInto(" and tag2 like ?", "%$tag2%");
		if($tag3!="全部")
			$where .= $db->quoteInto(" and tag3 like ?", "%$tag3%");
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
 	public function getIds($type){
    	$db = $this->getAdapter();
    	$result = $db->fetchAll("select vid from vod where type=:type",array("type"=>$type));
    	return $result;
    }
}
